package com.ccut.lc.controller;


import com.ccut.lc.model.Product;
import com.ccut.lc.model.ProductType;
import com.ccut.lc.model.QueryVo;
import com.ccut.lc.service.ProductListService;
import com.ccut.lc.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController{
	@Resource
	private ProductListService productListService;
//	@RequestMapping("add")
//	public String addProduct(Product product){
//		productListService.addProduct(product);
//		return "SCUESS";
//	}
	@RequestMapping("list")
	public String selProductList(QueryVo queryVo, Model model){
		Page<Product> page = productListService.selProductList(queryVo);
		List<ProductType> productType = productListService.findProductType();
		model.addAttribute("productType",productType);
		model.addAttribute("page",page);
		return "product/customer";
	}

	@RequestMapping("welcome")
	public String welcome(){
		return "system/welcome";
	}

	/**
	 * 获取用户列表
	 * @param
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/get_list",method= RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getList(Page page){
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> queryMap = new HashMap<String, Object>();
//		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());
//		ret.put("rows", productListService.selProductList(queryMap));
//		ret.put("total", productListService.selProductList(queryMap));
		return ret;
	}

	@RequestMapping("edit")
	@ResponseBody
	public Product edit(Integer id){
		Product product = productListService.findProductByID(id);
		return product;
	}

	@RequestMapping("update")
	@ResponseBody
	public String update(Product product){
		String msg = "1";
		try {
			productListService.updateProduct(product);
			msg = "0";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}
	@RequestMapping("delete")
	@ResponseBody
	public String delete(Integer id){
		String msg = "1";
		try {
			productListService.deleteProduct(id);
			msg = "0";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}
	@RequestMapping("addProduct")
	@ResponseBody
	public String add(Product product){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sf.format(new Date());
		String msg = "1";
		try {
			product.setProduct_time(format);
			productListService.addProduct(product);
			msg = "0";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	@RequestMapping("myTag")
	public String myTage() {
		return "myTag";
	}

}
